//
//  FGVideoMessageCell.m
//  FGChatKit
//
//  Created by Henry on 2021/6/3.
//

#import "FGVideoMessageCell.h"
#import "UIView+FGLayout.h"
#import "UIImageView+FGCornerRadius.h"
#import "FGResources.h"

@implementation FGVideoMessageCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _thumbView = [[UIImageView alloc] init];
        _thumbView.contentMode = UIViewContentModeScaleAspectFill;
        _thumbView.backgroundColor = [UIColor grayColor];
        _thumbView.layer.cornerRadius = 4;
        [_thumbView fg_cornerRadius:4];
        [self.container addSubview:_thumbView];
        _thumbView.mm_fill();
        _thumbView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

        CGSize playSize = CGSizeMake(40, 40);
        _playView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, playSize.width, playSize.height)];
        _playView.contentMode = UIViewContentModeScaleAspectFit;
        _playView.image = [FGResources getImageFromMessage:@"play_normal"];
        [self.container addSubview:_playView];

        _durationLabel = [[UILabel alloc] init];
        _durationLabel.textColor = [UIColor whiteColor];
        _durationLabel.font = [UIFont systemFontOfSize:12];
        [self.container addSubview:_durationLabel];

        _progressView = [[FGCircularProgressView alloc] init];
        _progressView.roundedCorners = YES;
        [self.container addSubview:_progressView];
    }
    return self;
}

- (void)fillWithData:(FGVideoMessageCellData *)data {
    [super fillWithData:data];
    
    self.videoData = data;
    
    _thumbView.image = nil;
    if (data.thumbImage == nil) {
        [data loadThumb];
    }
    
    [self.videoData addObserver:self forKeyPath:@"thumbImage" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld|NSKeyValueObservingOptionInitial context:nil];

    _durationLabel.text = [NSString stringWithFormat:@"%02ld:%02ld", (long)data.videoItem.duration / 60, (long)data.videoItem.duration % 60];
    
    if (data.direction == FGMsgDirectionIncoming) {
        [self.videoData addObserver:self forKeyPath:@"thumbProgress" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld|NSKeyValueObservingOptionInitial context:nil];
    } else {
        [self.videoData addObserver:self forKeyPath:@"uploadProgress" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld|NSKeyValueObservingOptionInitial context:nil];
    }

}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"thumbImage"]) {
        self.thumbView.image = self.videoData.thumbImage;
    } else if ([keyPath isEqualToString:@"thumbProgress"]) {
        CGFloat progress = self.videoData.thumbProgress / 100.f;
        [self.progressView setProgress:progress animated:YES];
        self.progressView.hidden = (progress >= 1 || progress == 0);
        self.playView.hidden = !self.progressView.hidden;
    } else if ([keyPath isEqualToString:@"uploadProgress"]) {
        CGFloat progress = self.videoData.uploadProgress / 100.f;
        [self.progressView setProgress:progress animated:YES];
        self.progressView.hidden = (progress >= 1 || progress == 0);
        self.playView.hidden = !self.progressView.hidden;
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.progressView.mm_center();
    self.playView.mm_center();
    self.durationLabel.mm_sizeToFit().mm_right(5).mm_bottom(5);
}

- (void)dealloc {
    if (self.videoData) {
        [self.videoData removeObserver:self forKeyPath:@"thumbImage" context:nil];
        if (self.videoData.direction == FGMsgDirectionIncoming) {
            [self.videoData removeObserver:self forKeyPath:@"thumbProgress" context:nil];
        } else {
            [self.videoData removeObserver:self forKeyPath:@"uploadProgress" context:nil];
        }
    }
}

@end
